PROJECT_NAME = s21_containers

CC = g++
CPP_FLAGS = -Wall -Wextra -std=c++17 -pedantic
GTEST_FLAGS = -lcheck -lm -lpthread -lgtest
GCOVR_FLAGS = --coverage -fprofile-arcs -ftest-coverage -O0 -fPIC
VALGRIND_FLAGS = --tool=memcheck --leak-check=full --show-leak-kinds=all --track-fds=yes --track-origins=yes
CPPCHECK_FLAGS = --enable=all --inconclusive --library=posix --suppress=missingIncludeSystem --std=c++17 --language=c++

SOURCE_DIR = ./$(PROJECT_NAME)
HEADERS = $(wildcard $(SOURCE_DIR)/*/*.h)
SOURCES = $(wildcard $(SOURCE_DIR)/*/*.cpp)
OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES))
TEST_DIR = ./tests
TEST_HEADERS = $(wildcard $(TEST_DIR)/*.h)
TEST_SOURCES = $(wildcard $(TEST_DIR)/*.cpp)
TEST_OBJECTS = $(patsubst %.cpp, %.o, $(TEST_SOURCES))
OS = $(shell uname -s)

ifeq ($(OS), Linux)
	GTEST_FLAGS += -lsubunit -lrt
endif

%.o: %.cpp
	$(CC) -c $(CPP_FLAGS) $< -o $@ $(GCOVR_FLAGS)

all: clean format leaks gcov_report

build_test:
	$(CC) $(CPP_FLAGS) $(TEST_SOURCES) $< -o test.out $(GTEST_FLAGS)

test: build_test
	./test.out

gcov_report: $(OBJECTS) ${TEST_OBJECTS}
	$(info ********************    gcov_report     ********************)
	mkdir -p ./report
	$(CC) $(CPP_FLAGS) -o $@.out $^ $(GTEST_FLAGS) $(GCOVR_FLAGS)
	./$@.out
	gcovr -r . --html --html-details -o ./report/coverage-report.html
	google-chrome ./report/coverage-report.html

clean:
	$(info ********************       clean        ********************)
	find ./ \( -name "*.o" -o -name "*.a" -o -name "*.out" \) -delete
	find ./ \( -name "*.gch" -o -name "*.gcda" -o -name "*.gcno" -o -name "*.gcov" \) -delete
	rm -rf ./report

format:
	$(info ********************       format       ********************)
	clang-format -i -style=Google $(HEADERS) $(SOURCES) ./*.h $(TEST_HEADERS) $(TEST_SOURCES)

leaks: build_test
	$(info ********************       leaks        ********************)
	valgrind $(VALGRIND_FLAGS) ./test.out